<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospset.hosname"/>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospset.hoscode"/>
      </el-form-item>
      <el-form-item label="api地址">
        <el-input v-model="hospset.apiUrl"/>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospset.contactsName"/>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospset.contactsPhone"/>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import hospsetApi from "@/api/yygh/hospset";

export default {
  data(){
    return{
      id:'',
      hospset:{},//表单对象
      saveBtnDisabled: false // 保存按钮是否禁用,
    }
  },
  created(){
    
    if(this.$route.params && this.$route.params.id){
          console.log(this.$route.params.id)

          this.id=this.$route.params.id
          hospsetApi.getHospSetById(this.id)
          .then(response=>{
            this.hospset = response.data.hospitalSet
          })
    }
  },
  methods:{
    //保存方法
    saveOrUpdate(){
      this.saveBtnDisabled = true;
      if (this.hospset.id) {
        //修改
        this.updateInfo()
      } else {
        //新增
        this.saveInfo();
      }
    },
    //保存新增
     saveInfo(){
        hospsetApi
        .save(this.hospset)
        .then(response=>{
           this.$message({
            type: "success",
            message: "添加成功!",
          });
          this.saveBtnDisabled= false
          //跳转到列表页面
          this.$router.push({path:'/yygh/hospset/list'})

        })
    },
    updateInfo(){
      hospsetApi
      .update(this.hospset)
      .then(response=>{
        this.$message({
            type: "success",
            message: "修改成功!",
          });
          this.saveBtnDisabled= false
          //跳转到列表页面
          this.$router.push({path:'/yygh/hospset/list'})
      })
    }
  }
}
</script>
